<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>聊天室</title>
  <link rel="stylesheet" type="text/css" href="css/index.css"/>
</head>
<body>
<div class="main">
  <div class="main-top">
    socket.io demo
  </div>
  <div class="main-body">
    <section class="chatRoomInfo">
      <div class="info">当前共有<span class="chatNum">0</span>人在线。在线列表:&nbsp;<span class="chatList"></span></div>
    </section>
    <!--<section class="chatRoomTip">
        <div>子木加入到聊天室</div>
    </section>
    <section class="user clearfix">
        <span>子木</span>
        <div>
            测试测试测试测试测试测试测试测试测试试测试测试测试测试测试测试测试测试测试测试测试
        </div>
    </section>
    <section class="server clearfix">
        <span>子木</span>
        <div>
            测试测试测试
        </div>
    </section>-->
  </div>
  <div class="main-footer clearfix">
    <div class="input">
      <input type="text" name="msg" id="msg" value="" />
    </div>
    <button type="button" class="send">发送</button>
  </div>
</div>
<script src="/javascripts/jquery-2.1.1.js" type="text/javascript" charset="utf-8"></script>
<script src="socket.io/socket.io.js" type="text/javascript" charset="utf-8"></script>
<script>
  /*按钮点击效果*/
  $('.send').mousedown(function(){
    $(this).css({'background':"#007aff",'color':"#ffffff"});
  })
  $('.send').mouseup(function(){
    $(this).css({'background':"#e8e8e8",'color':"#ffffff"});
  })
  /*socket*/
  window.onload=function () {
    var username=prompt('请输入您的姓名');
    if (!username){
      alert('姓名必填');
      history.go(0);
    }
//          username="子木";
    userId=genUid();
    var userInfo={
      'id':username,
      'username':username,
      "roomType":2
    };
    //连接socket后端服务器
    var socket=io.connect("ws://127.0.0.1:8081");
    //通知用户有用户登录
    socket.emit('inRoom',userInfo);
    //监听新用户登录
    console.log("44444444444444444444444444",userInfo)

    socket.on('numChange',function (o) {
      console.log("3333333344444444444444444",o)
      updateMsg(o, 'login');
    });

    socket.on('login',function (o) {
      console.log("loginloginloginloginloginloginlogin",o)
      updateMsg(o, 'login');
    });
    //监听用户退出
    socket.on('logout',function (o) {
      updateMsg(o, 'logout');
    });


    socket.on('questionInfo',function (obj) {
      console.log("questionInfoquestionInfoquestionInfoquestionInfo",obj)
      if(obj.userid==userId) {
        var MsgHtml='<section class="user clearfix">'
          +'<span>'+obj.question+'</span>'
          +'<div>'+obj.answerList+'</div>'
          +'</section>';
      }else{
        var MsgHtml='<section class="server clearfix">'
          +'<span>'+obj.question+'</span>'
          +'<div>'+obj.answerList+'</div>'
          +'</section>';
      }
      $('.main-body').append(MsgHtml);
      $('.main-body').scrollTop(99999);
    });

    socket.on('answerInfo',function (obj) {
      console.log("answerInfoanswerInfoanswerInfoanswerInfo",obj)
      if(obj.userid==userId) {
        var MsgHtml='<section class="user clearfix">'
          +'<div>'+obj.answer+'</div>'
          +'</section>';
      }else{
        var MsgHtml='<section class="server clearfix">'
          +'<div>'+obj.answer+'</div>'
          +'</section>';
      }
      $('.main-body').append(MsgHtml);
      $('.main-body').scrollTop(99999);
    });
    //发送消息
    socket.on('message',function (obj) {
      if(obj.userid==userId) {
        var MsgHtml='<section class="user clearfix">'
          +'<span>'+obj.username+'</span>'
          +'<div>'+obj.content+'</div>'
          +'</section>';
      }else{
        var MsgHtml='<section class="server clearfix">'
          +'<span>'+obj.username+'</span>'
          +'<div>'+obj.content+'</div>'
          +'</section>';
      }
      $('.main-body').append(MsgHtml);
      $('.main-body').scrollTop(99999);
    })
    $('.send').click(function () {
      var content=$('input[name="msg"]').val();
      if (content){
        var obj={
          'userid':userId,
          'username':username,
          "roomType":2,
          'content':content
        }
        socket.emit('answer',obj);
        $('input[name="msg"]').val("");
      }
    })

  }

  /*用户id生成*/
  function  genUid() {
    return new Date().getTime()+""+Math.floor(Math.random()*899+100);
  }
  function logout(){
    socket.disconnect();
    location.reload();
  }
  /*监听函数*/
  function updateMsg(o,action) {
    //当前在线列表
    var onlineUser=o.count;
    //当前在线数
    var onlineCount=o.count;
    //新加用户
    var user= 111;
    //更新在线人数
    var userList='';
    var separator = '';
    for(key in onlineUser){
      userList+=separator+onlineUser[key];
      separator = '、';
    }
    //跟新房间信息
    $('.chatNum').text(onlineCount);
    $('.chatList').text(userList);
    //系统消息
    if(action=='login'){
      var sysHtml='<section class="chatRoomTip"><div>'+user+'进入聊天室</div></section>';
    }
    if(action=="logout"){
      var sysHtml='<section class="chatRoomTip"><div>'+user+'退出聊天室</div></section>';
    }
    $(".main-body").append(sysHtml);
    $('.main-body').scrollTop(99999);
  }

  //do something</script>
</body>
</html>